package com.example.shopping2.a_c_fragment.car

import BaseActivity
import IActivity
import android.content.Intent
import android.text.TextUtils
import android.widget.AdapterView
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatButton
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.shopping2.R
import com.example.shopping2.a_a_entity.BaseEntity
import com.example.shopping2.a_a_mvp.contract.HomeContract
import com.example.shopping2.a_a_mvp.model.models.HomeModel
import com.example.shopping2.a_a_mvp.presenter.presenters.HomePresenter
import com.example.shopping2.a_c_fragment.car.adapter.Order_Adapter
import com.example.shopping2.a_c_fragment.car.entity.OrderGoodsEntity
import com.example.shopping2.a_c_network.HttpCode
import com.example.shopping2.a_c_network.LjlUtils
import java.sql.DriverManager

class ComOrderActivity : BaseActivity<HomePresenter>(), HomeContract.HomeView, IActivity {
    private val orderBack: ImageView by lazy { findViewById<ImageView>(R.id.order_back) }
    private val orderPrice: TextView by lazy { findViewById<TextView>(R.id.order_price) }
    private val orderRec: RecyclerView by lazy { findViewById<RecyclerView>(R.id.order_rec) }
    private val orderCommit: AppCompatButton by lazy { findViewById<AppCompatButton>(R.id.order_commit) }

    private var orderid = 0
    private var goodssum = 0

    private var address = OrderGoodsEntity.DataBean.ShipAddressBean()


    override fun findlayout(): Int {
        return R.layout.order_activity
    }

    override fun initView() {
        orderid = intent.getIntExtra("data", 0)

        orderBack.setOnClickListener {
            finish()
        }
        orderCommit.setOnClickListener {
            if (TextUtils.isEmpty(address.ship_user_name)){
                LjlUtils.Ljl_Toast(this,"请选择收货地址")
            }

        }

    }

    override fun initData() {
        mypre = HomePresenter(HomeModel(), this)
        //获取订单
        var list: ArrayList<HashMap<String, Any>> = ArrayList()
        var map: HashMap<String, Any> = HashMap()
        map.put("code", HttpCode.ORDER)
        map.put("orderId", orderid)
        list.add(map)
        mypre.requestAll(list)

    }

    override fun showHomeData(baseEntity: BaseEntity) {
        if (baseEntity is OrderGoodsEntity) {
            var data = baseEntity.data.orderGoodsList
            var adapter =
                Order_Adapter(data as ArrayList<OrderGoodsEntity.DataBean.OrderGoodsListBean>)
            orderRec.adapter = adapter
            orderRec.layoutManager = LinearLayoutManager(this)
            orderRec.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))

            var head = LinearLayout.inflate(this, R.layout.view_confirm_head, null)
            var mSelectShipTv = head.findViewById<TextView>(R.id.mSelectShipTv)
            mSelectShipTv.setOnClickListener {
                //跳转到收货地址
                startActivity(Intent(this,AddressListActivity::class.java))
            }

            adapter.addHeaderView(head)


            var foot = LinearLayout.inflate(this, R.layout.view_confirm_foot, null)
            adapter.addFooterView(foot)

            //获取价钱
            var totalPrice = baseEntity.data.totalPrice.toInt()
            //获取件数
            for (i in 0..data.size - 1) {
                var goodsCount = data[i].goods_count
                goodssum += goodsCount
            }
            orderPrice.setText("共" + goodssum + "件 , 共计:￥" + totalPrice / 100)

        }
    }
}